package com.kk.permission.huawei;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Handler;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import com.kk.permission.AccessibilityEventType;
import com.kk.permission.GuideConst;
import com.kk.permission.IPermissionGuideStrategy;
import com.kk.permission.accessibilityutils.NodeUtil;
import com.kk.permission.utils.callershow.CallerShowUtils;

public abstract class HuaweiPermissionStrategyBase extends IPermissionGuideStrategy {
    public static int mVersionNum;
    public String mAppName = HuaweiTool.getAppName();
    public boolean mAutoGuide = CallerShowUtils.queryAutoPermission();
    private AccessibilityEventType mEventType = AccessibilityEventType.DEFAULT;
    public HuaweiPermissionAccessbilityHandlerBase mHandlerBase;
    public int mVersion;

    public abstract HuaweiPermissionAccessbilityHandlerBase getHandlerBase();

    public HuaweiPermissionStrategyBase(Context context) {
        super(context);
    }

    public void handleAccessbilityEvent(AccessibilityEvent accessibilityEvent, AccessibilityService accessibilityService) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("");
        stringBuilder.append(this.mEventType);
        Log.e("zhaoyanjun:han", stringBuilder.toString());
        if (this.mHandlerBase != null && accessibilityService != null) {
            this.mHandlerBase.handleAccessbilityEvent(accessibilityEvent, accessibilityService, this.mEventType);
        }
    }

    public void configAccessbility(AccessibilityService accessibilityService) {
        if (accessibilityService != null) {
            if (this.mHandlerBase == null) {
                this.mHandlerBase = getHandlerBase();
            }
            AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
            accessibilityServiceInfo.packageNames = new String[]{"com.huawei.systemmanager", GuideConst.ANDROID_PACKAGE_INSTALLER_PACKAGE_NAME, "com.android.settings"};
            accessibilityServiceInfo.eventTypes = -1;
            accessibilityServiceInfo.feedbackType = 16;
            accessibilityServiceInfo.notificationTimeout = 1000;
            accessibilityServiceInfo.flags = 16 | accessibilityServiceInfo.flags;
            accessibilityService.setServiceInfo(accessibilityServiceInfo);
            NodeUtil.back(accessibilityService);
        }
    }

    public static int getHuaweiSystemManagerVersion(Context context)
    {   int i=0;
        int j=0;
        int k=0;
        try
        {

            String[] paramContext = context.getPackageManager().getPackageInfo("com.huawei.systemmanager", 0).versionName.split("\\.");
            if (paramContext.length >= 2)
            {
                if (Integer.parseInt(paramContext[0]) == 9)
                {
                    i = 900;
                }
                else if (Integer.parseInt(paramContext[0]) == 8)
                {
                    i = 800;
                }
                else if (Integer.parseInt(paramContext[0]) == 5)
                {
                    i = 500;
                }
                else
                {
                    StringBuilder localStringBuilder;
                    if (Integer.parseInt(paramContext[0]) == 4)
                    {
                        localStringBuilder = new StringBuilder();
                        localStringBuilder.append(paramContext[0]);
                        localStringBuilder.append(paramContext[1]);
                        localStringBuilder.append(paramContext[2]);
                        i = Integer.parseInt(localStringBuilder.toString());
                    }
                    else
                    {
                        localStringBuilder = new StringBuilder();
                        localStringBuilder.append(paramContext[0]);
                        localStringBuilder.append(paramContext[1]);
                        i = Integer.parseInt(localStringBuilder.toString());
                    }
                }
            }
            else {
                i = 0;
            }
            j = i;
            try
            {
                if (paramContext.length < 3) {
                    return i;
                }
                k = Integer.valueOf(paramContext[2].substring(0, 1)).intValue();
            }
            catch (Exception e) {}
        }
        catch (Exception paramContext)
        {

        }
        j = i;
        i = j;
        mVersionNum = i;
        if (i >= 330)
        {
            if (i >= 900) {}
            while (i >= 800) {
                return 9;
            }
            if (i >= 500) {
                return 6;
            }
            if (i >= 400) {
                return 5;
            }
            if (i >= 331) {
                return 4;
            }
            if ((k != 6) && (k != 4) && (k != 2)) {
                return 2;
            }
            return 3;
        }
        if (i != 0) {
            return 1;
        }
        return 0;
    }

    public boolean supportAutoPermission() {
        boolean z = true;
        if (mVersionNum == 900) {
            return true;
        }
        if (getHuaweiSystemManagerVersion(this.mContext) != 9) {
            return super.supportAutoPermission();
        }
        if (mVersionNum != 800) {
            z = false;
        }
        return z;
    }

    public int accessbilityToastType() {
        return VERSION.SDK_INT >= 26 ? 2038 : 2002;
    }

    protected void actionToastPermission() {
        super.actionToastPermission();
        setEventType(AccessibilityEventType.TOAST);
    }

    protected void actionDialNotiPermission() {
        super.actionDialNotiPermission();
        setEventType(AccessibilityEventType.DIALNOTI);
    }

    protected void actionCallRingtonePermission() {
        super.actionCallRingtonePermission();
        setEventType(AccessibilityEventType.CALLRINGTONE);
    }

    protected void actionBackgroundPermisssion() {
        super.actionBackgroundPermisssion();
        setEventType(AccessibilityEventType.BACKGROUNDPROTECT);
    }

    protected void actionAutoBootPermission() {
        super.actionAutoBootPermission();
        setEventType(AccessibilityEventType.AUTOBOOT);
    }

    protected void actionTrustApplicationPermission(boolean z) {
        super.actionTrustApplicationPermission(z);
        setEventType(AccessibilityEventType.TRUST_APP);
    }

    protected void actionInstallShortCut() {
        super.actionInstallShortCut();
        setEventType(AccessibilityEventType.SHORTCUT);
        Intent intent = new Intent();
        intent.setClassName("com.huawei.systemmanager", GuideConst.HUAWEI_APP_PERMISSION_ACTIVITY_NAME);
        this.mContext.startActivity(intent);
    }

    protected void actionSystemDialingPermission() {
        super.actionSystemDialingPermission();
        setEventType(AccessibilityEventType.SYSTEMDIALING);
    }

    public void setEventType(AccessibilityEventType accessibilityEventType) {
        Log.e("ttt", "setEventType: "+ accessibilityEventType);
        this.mEventType = accessibilityEventType;
    }

    public void startActivity(final Intent intent, long j) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                HuaweiPermissionStrategyBase.this.mContext.startActivity(intent);
            }
        });
//        UiThreadExecutor.execute(new Runnable() {
//            public void run() {
//                HuaweiPermissionStrategyBase.this.mContext.startActivity(intent);
//            }
//        }, j);
    }
}
